<?php
//注册插件
RegisterPlugin("SidebarDIY","ActivePlugin_SidebarDIY");
    
function ActivePlugin_SidebarDIY() {
	Add_Filter_Plugin('Filter_Plugin_Zbp_Load','SidebarDIY_Main');
	Add_Filter_Plugin('Filter_Plugin_Admin_TopMenu','SidebarDIY_AddMenu');

}

function SidebarDIY_Main() {
	global $zbp;
	$ILL_COMMENTS_LEN = $zbp->Config('SidebarDIY')->ILL_COMMENTS_LEN;

	$zbp->RegBuildModule('catalog','BuildModule_SidebarDIYCatalog');
	$zbp->RegBuildModule('calendar','BuildModule_SidebarDIYCalendar');
	$zbp->RegBuildModule('comments','BuildModule_SidebarDIYcomments');
	$zbp->RegBuildModule('archives','BuildModule_SidebarDIYarchives');
	$zbp->RegBuildModule('tags','BuildModule_SidebarDIYtags');
}



function SidebarDIY_AddMenu(&$m){
	global $zbp;
	$m[]=MakeTopMenu("root",'SidebarDIY侧边栏配置(Free)',$zbp->host . "zb_users/plugin/SidebarDIY/main.php?act=config","","topmenu_SidebarDIY");
}


//*******************************************************
//*******CutTitle 限制文字长度
//*****作者不详
//*****Utf-8、gb2312都支持的汉字截取函数
//*****cut_str(字符串, 截取长度, 开始长度, 编码);
//*****编码默认为 utf-8
//*****开始长度默认为 0
//*******************************************************
function SidebarDIY_cutTitle($string,$length)
{
    $result= preg_replace('/[\r\n\s]+/', '', trim(SubStrUTF8(TransferHTML($string,'[nohtml]'),$length)).'...');
    return $result;
}

//*******************************************************
//*******Catalog 分类 前带有图片
//*******************************************************
    function BuildModule_SidebarDIYCatalog() {
global $zbp;
	$s = '';

	if ($zbp->option['ZC_MODULE_CATALOG_STYLE'] == '2') {

		foreach ($zbp->categorysbyorder as $key => $value) {
			if ($value->Level == 0) {
				$s .= '<li class="li-cate"><a href="' . $value->Url . '">' . $value->Name . '</a><!--' . $value->ID . 'begin--><!--' . $value->ID . 'end--></li>';
			}
		}
		foreach ($zbp->categorysbyorder as $key => $value) {
			if ($value->Level == 1) {
				$s = str_replace('<!--' . $value->ParentID . 'end-->', '<li class="li-subcate"><a href="' . $value->Url . '">' . $value->Name . '</a><!--' . $value->ID . 'begin--><!--' . $value->ID . 'end--></li><!--' . $value->ParentID . 'end-->', $s);
			}
		}
		foreach ($zbp->categorysbyorder as $key => $value) {
			if ($value->Level == 2) {
				$s = str_replace('<!--' . $value->ParentID . 'end-->', '<li class="li-subcate"><a href="' . $value->Url . '">' . $value->Name . '</a><!--' . $value->ID . 'begin--><!--' . $value->ID . 'end--></li><!--' . $value->ParentID . 'end-->', $s);
			}
		}
		foreach ($zbp->categorysbyorder as $key => $value) {
			if ($value->Level == 3) {
				$s = str_replace('<!--' . $value->ParentID . 'end-->', '<li class="li-subcate"><a href="' . $value->Url . '">' . $value->Name . '</a><!--' . $value->ID . 'begin--><!--' . $value->ID . 'end--></li><!--' . $value->ParentID . 'end-->', $s);
			}
		}

		foreach ($zbp->categorysbyorder as $key => $value) {
			$s = str_replace('<!--' . $value->ID . 'begin--><!--' . $value->ID . 'end-->', '', $s);
		}
		foreach ($zbp->categorysbyorder as $key => $value) {
			$s = str_replace('<!--' . $value->ID . 'begin-->', '<ul class="ul-subcates">', $s);
			$s = str_replace('<!--' . $value->ID . 'end-->', '</ul>', $s);
		}

	} elseif ($zbp->option['ZC_MODULE_CATALOG_STYLE'] == '1') {
		foreach ($zbp->categorysbyorder as $key => $value) {
			$s .= '<li>' . $value->Symbol . '<a href="' . $value->Url . '">' . $value->Name . '</a></li>';
		}
	} else {
		foreach ($zbp->categorysbyorder as $key => $value) {
			$s .= '<li><span class="feed-icon"><img title="'.$value->Name.'" style="width:16px;height:16px;vertical-align:middle;border:0;margin:2px 0px 2px 0px;" src="{$host}zb_users/plugin/SidebarDIY/icons/'. $value->ID .'.png" alt="'.$value->Name.'" /></span>&nbsp;&nbsp;&nbsp;<a href="' . $value->Url . '">' . $value->Name .'&nbsp;&nbsp;('.$value->Count . ')</a></li>';
		}
	}

		

	return $s;

}

//*******************************************************
//*******WPcalendar
//*******************************************************
    function BuildModule_SidebarDIYCalendar($date = '') {
   	global $zbp;

	if ($date == '')
		$date = date('Y-m', time());
  $s = '';
	$s .= '<table id="wp-calendar"><caption>';

	$url = new UrlRule($zbp->option['ZC_DATE_REGEX']);
	$value = strtotime('-1 month', strtotime($date));
	$url->Rules['{%date%}'] = date('Y-n', $value);
	$url->Rules['{%year%}'] = date('Y', $value);
	$url->Rules['{%month%}'] = date('n', $value);

	$url->Rules['{%day%}'] = 1;
	$s .= '<a href="' . $url->Make() . '" title=查看' . date('n', $value) . '月的日志>«</a>';
  //$s .= '<a href="' . $url->Make() . '">«</a>';

	$value = strtotime($date);
	$url->Rules['{%date%}'] = date('Y-n', $value);
	$url->Rules['{%year%}'] = date('Y', $value);
	$url->Rules['{%month%}'] = date('n', $value);
	$s .= '&nbsp;&nbsp;&nbsp;<a href="' . $url->Make() . '">' . str_replace(array('%y%', '%m%'), array(date('Y', $value), date('n', $value)), $zbp->lang['msg']['year_month']) . '</a>&nbsp;&nbsp;&nbsp;';

	$value = strtotime('+1 month', strtotime($date));
	$url->Rules['{%date%}'] = date('Y-n', $value);
	$url->Rules['{%year%}'] = date('Y', $value);
	$url->Rules['{%month%}'] = date('n', $value);
	$s .= '<a href="' . $url->Make() . '" title="查看' . date('n', $value) . '月的日志">»</a></caption>';
	//$s .= '<a href="' . $url->Make() . '">»</a></caption>';


  $s .= '<thead>	<tr> <th title="Monday" scope="col">一</th> <th title="Tuesday" scope="col">二</th>	<th title="Wednesday" scope="col">三</th> <th title="Thursday" scope="col">四</th>	<th title="Friday" scope="col">五</th> <th title="Saturday" scope="col">六</th> <th title="Sunday" scope="col">日</th>	</tr>	</thead>';
	$s .= '<tbody class="month' . date('n',strtotime($date)) . '">';
	//$s .= '<tbody>';
	$s .= '<tr>';

	$a = 1;
	$b = date('t', strtotime($date));
	$j = date('N', strtotime($date . '-1'));
	$k = 7 - date('N', strtotime($date . '-' . date('t', strtotime($date))));
	
	$nobirdm = date('m', strtotime($date));//数字表示的月份，有前导零
	$nobirdY = date('Y', strtotime($date));//4 位数字完整表示的年份

	if ($j > 1) {
		$s .= '<td class="pad" colspan="' . ($j - 1) . '"> </td>';
	} elseif ($j = 1) {
		$s .= '';
	}

	$l = $j - 1;
	for ($i = $a; $i < $b + 1; $i++) {
		$s .= '<td';
//如果是今天  特殊效果
	  $strtoday=date('Y-m-j');
	  $stristoday=$nobirdY."-".$nobirdm."-".$i;
		if ($strtoday==$stristoday){
		$s .=' id="today"';}
		
		$s .='>' . $i . '</td>';

		$l = $l + 1;
		if ($l % 7 == 0)
			$s .= '</tr><tr>';
	}

	if ($k > 1) {
		$s .= '<td class="pad" colspan="' . ($k) . '"> </td>';
	} elseif ($k = 1) {
		$s .= '';
	}

	$s .= '</tr></tbody>';
	$s .= '</table>';
	$s = str_replace('<tr></tr>', '', $s);

  $fdate = strtotime($date);
	$ldate = (strtotime(date('Y-m-t', strtotime($date))) + 60 * 60 * 24);
	$sql = $zbp->db->sql->Select(
		$zbp->table['Post'],
		array('log_ID', 'log_PostTime'),
		array(
			array('=', 'log_Type', '0'),
			array('=', 'log_Status', '0'),
			array('BETWEEN', 'log_PostTime', $fdate, $ldate)
		),
		array('log_PostTime' => 'ASC'),
		null,
		null
	);
	$array = $zbp->db->Query($sql);
	$arraydate = array();
	$arrayid = array();
	foreach ($array as $key => $value) {
		$arraydate[date('j', $value['log_PostTime'])] = $value['log_ID'];
	}
	if (count($arraydate) > 0) {
		foreach ($arraydate as $key => $value) {
			$arrayid[] = array('log_ID', $value);
		}
		$articles = $zbp->GetArticleList('*', array(array('array', $arrayid)),null,null,null,false);
		foreach ($arraydate as $key => $value) {
			$a = $zbp->GetPostByID($value);
			$s = str_replace('<td>' . $key . '</td>', '<td><a href="' . $a->Url . '">' . $key . '</a></td>', $s);
		}
	}

	return $s;
    }

//*******************************************************
//*******评论增加姓名
//*******************************************************
function BuildModule_SidebarDIYcomments() {
	global $zbp;

	$i = $zbp->modulesbyfilename['comments']->MaxLi;
	if ($i == 0) $i = 10;
	$comments = $zbp->GetCommentList('*', array(array('=', 'comm_IsChecking', 0)), array('comm_PostTime' => 'DESC'), $i, null);

	$s = '';
	foreach ($comments as $comment) {
//		$s .= '<li><a href="' . $comment->Post->Url . '#cmt' . $comment->ID . '" title="' . htmlspecialchars($comment->Author->StaticName . ' @ ' . $comment->Time()) . '">' . TransferHTML($comment->Content, '[noenter]') . '</a></li>';
  $nobirdcauthor = $comment->Author->Name;
  $nobirdccontent = $comment->Content;
  $nobirdccontentnew = $nobirdcauthor.'：'.$nobirdccontent;
  $nobirdccontentend = SidebarDIY_cutTitle($nobirdccontentnew,$zbp->Config('SidebarDIY')->ILL_COMMENTS_LEN);
  
    $s .= '<li><a href="' . $comment->Post->Url . '#cmt' . $comment->ID . '" title="' . htmlspecialchars($comment->Author->StaticName . ' @ ' . $comment->Time()) . '">' . $nobirdccontentend . '</a></li>';
	

	}

	return $s;
}



//*******************************************************
//*******日期归档 下拉菜单
//*******************************************************
function BuildModule_SidebarDIYarchives() {
	global $zbp;

	$i = $zbp->modulesbyfilename['archives']->MaxLi;
	if($i<0)return '';

	$fdate;
	$ldate;

	$sql = $zbp->db->sql->Select($zbp->table['Post'], array('log_PostTime'), null, array('log_PostTime' => 'DESC'), array(1), null);

	$array = $zbp->db->Query($sql);

	if (count($array) == 0)
		return '';

	$ldate = array(date('Y', $array[0]['log_PostTime']), date('m', $array[0]['log_PostTime']));

	$sql = $zbp->db->sql->Select($zbp->table['Post'], array('log_PostTime'), null, array('log_PostTime' => 'ASC'), array(1), null);

	$array = $zbp->db->Query($sql);

	if (count($array) == 0)
		return '';

	$fdate = array(date('Y', $array[0]['log_PostTime']), date('m', $array[0]['log_PostTime']));

	$arraydate = array();

	for ($i = $fdate[0]; $i < $ldate[0] + 1; $i++) {
		for ($j = 1; $j < 13; $j++) {
			$arraydate[] = strtotime($i . '-' . $j);
		}
	}

	foreach ($arraydate as $key => $value) {
		if ($value - strtotime($ldate[0] . '-' . $ldate[1]) > 0)
			unset($arraydate[$key]);
		if ($value - strtotime($fdate[0] . '-' . $fdate[1]) < 0)
			unset($arraydate[$key]);
	}

	$arraydate = array_reverse($arraydate);

	$s = '';
  $s .='<li><select onchange="window.location=this.options[this.selectedIndex].value;"><option value="{$host}">浏览归档日志...</option>';
	foreach ($arraydate as $key => $value) {
		$url = new UrlRule($zbp->option['ZC_DATE_REGEX']);
		$url->Rules['{%date%}'] = date('Y-n', $value);
		$url->Rules['{%year%}'] = date('Y', $value);
		$url->Rules['{%month%}'] = date('n', $value);
		$url->Rules['{%day%}'] = 1;

		$fdate = $value;
		$ldate = (strtotime(date('Y-m-t', $value)) + 60 * 60 * 24);
		$sql = $zbp->db->sql->Count($zbp->table['Post'], array(array('COUNT', '*', 'num')), array(array('=', 'log_Type', '0'), array('=', 'log_Status', '0'), array('BETWEEN', 'log_PostTime', $fdate, $ldate)));
		$n = GetValueInArrayByCurrent($zbp->db->Query($sql), 'num');
		if ($n > 0) {
//			$s .= '<li><a href="' . $url->Make() . '">' . str_replace(array('%y%', '%m%'), array(date('Y', $fdate), date('n', $fdate)), $zbp->lang['msg']['year_month']) . ' (' . $n . ')</a></li>';
      $s .= '<option value="' . $url->Make() . '">' . str_replace(array('%y%', '%m%'), array(date('Y', $fdate), date('n', $fdate)), $zbp->lang['msg']['year_month']) . ' (' . $n . ')</option>';

		}
	}
	$s .= '</select></li>';

	return $s;

}



//*******************************************************
//*******热门tags(top 25) 附加彩色效果
//*******************************************************
function BuildModule_SidebarDIYtags() {
	global $zbp;
	$s = '';
	$i = $zbp->modulesbyfilename['tags']->MaxLi;
	if ($i == 0) $i = 25;
	$array = $zbp->GetTagList('*', '', array('tag_Count' => 'DESC'), $i, null);
	$array2 = array();
	foreach ($array as $tag) {
		$array2[$tag->ID] = $tag;
	}
	ksort($array2);

	foreach ($array2 as $tag) {
	
$color1 = '#'.sprintf('%02X',rand(0,255)).sprintf('%02X',rand(0,255)).sprintf('%02X',rand(0,255));	

	
		$s .= '<li><a href="' . $tag->Url . '" style="color:'.$color1.';">' . $tag->Name . '<span class="tag-count"> (' . $tag->Count . ')</span></a></li>';
	}

	return $s;
}


    function InstallPlugin_SidebarDIY(){
        global $zbp;
	if(!$zbp->Config('SidebarDIY')->HasKey('Version')) {
	$zbp->Config('SidebarDIY')->Version = '1.0';
	$zbp->Config('SidebarDIY')->ILL_COMMENTS_LEN = '20';
		$zbp->SaveConfig('SidebarDIY');
	}

       	$zbp->AddBuildModule('catalog');
       	$zbp->AddBuildModule('calendar');
       	$zbp->AddBuildModule('comments');
       	$zbp->AddBuildModule('archives');
       	$zbp->AddBuildModule('tags');

    }

    function UninstallPlugin_SidebarDIY(){
        global $zbp;
  $zbp->DelConfig('SidebarDIY');
	$zbp->RegBuildModule('catalog','BuildModule_catalog');
	$zbp->RegBuildModule('calendar','BuildModule_calendar');
	$zbp->RegBuildModule('comments','BuildModule_comments');
	$zbp->RegBuildModule('archives','BuildModule_archives');
	$zbp->RegBuildModule('tags','BuildModule_tags');
       	$zbp->AddBuildModule('catalog');
       	$zbp->AddBuildModule('calendar');
       	$zbp->AddBuildModule('comments');
       	$zbp->AddBuildModule('archives');
       	$zbp->AddBuildModule('tags');

    }
 
?>